import app from "./app";
// import * as open from "open";
import config from "./config";
import * as dayjs from "dayjs";
import * as multer from "multer";
import Logger from "./loaders/logger";
const expressSwagger = require("express-swagger-generator")(app);
expressSwagger(config.options);


import {
  login,
  register,
  updateList,
  deleteList,
  searchPage_BeingContractList,
  searchPage_ContractList,
  searchPage_servelist,
  searchPage_serverrecordlist,
  detailsList_Contract,
  detailsList_Server,
  UpdateDetails_Contract,
  UpdateDetails_Server,
  searchVague,
  upload,
  captcha,
} from "./router/http";

// app.post("/login", (req, res) => {
//   login(req, res);
// });

// app.post("/register", (req, res) => {
//   register(req, res);
// });

// app.put("/updateList/:id", (req, res) => {
//   updateList(req, res);
// });

// app.delete("/deleteList/:id", (req, res) => {
//   deleteList(req, res);
// });

// app.post("/searchPage", (req, res) => {
//   searchPage(req, res);
// });

// app.post("/searchVague", (req, res) => {
//   searchVague(req, res);
// });

//待处理签约 分页请求
app.post("/ContractManage/BeingContractList", (req, res) => {
  // console.log(111);
  searchPage_BeingContractList(req, res);
});
//签约记录 分页请求
app.post("/ContractManage/ContractList", (req, res) => {
  searchPage_ContractList(req, res);
});
//待处理服务 分页请求
app.post("/servemanage/servelist", (req, res) => {
  searchPage_servelist(req, res);
});
//服务记录 分页请求
app.post("/servemanage/serverrecordlist", (req, res) => {
  searchPage_serverrecordlist(req, res);
});



//签约管理 用户详细数据查询
app.post("/ContractManage/details", (req, res) => {
  detailsList_Contract(req, res);
});

//服务管理 用户详细数据查询
app.post("/servemanage/server", (req, res) => {
  detailsList_Server(req, res);
});

//签约管理 用户详细数据修改保存
app.put("/ContractManage/save", (req, res) => {
  UpdateDetails_Contract(req, res);
});

//服务管理 用户详细数据修改保存
app.put("/servemanage/save", (req, res) => {
  UpdateDetails_Server(req, res);
});

// 新建存放临时文件的文件夹
const upload_tmp = multer({ dest: "upload_tmp/" });
app.post("/upload", upload_tmp.any(), (req, res) => {
  upload(req, res);
});

app.get("/captcha", (req, res) => {
  captcha(req, res);
  // console.log(res.json);
  
});

app.ws("/socket", function (ws, req) {
  ws.send(
    `${dayjs(new Date()).format("YYYY年MM月DD日HH时mm分ss秒")}成功连接socket`
  );

  // 监听客户端是否关闭socket
  ws.on("close", function (msg) {
    console.log("客户端已关闭socket", msg);
    ws.close();
  });

  // 监听客户端发送的消息
  ws.on("message", function (msg) {
    // 如果客户端发送close，服务端主动关闭该socket
    if (msg === "close") ws.close();

    ws.send(
      `${dayjs(new Date()).format(
        "YYYY年MM月DD日HH时mm分ss秒"
      )}接收到客户端发送的信息，服务端返回信息：${msg}`
    );
  });
});

app
  .listen(config.port, () => {
    Logger.info(`
    ################################################
    🛡️  Swagger文档地址: http://localhost:${config.port} 🛡️
    ################################################
  `);
  })
  .on("error", (err) => {
    Logger.error(err);
    process.exit(1);
  });

// open(`http://localhost:${config.port}`); // 自动打开默认浏览器
